package com.example.lenovo.a32ui;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class ProgressBarTest extends AppCompatActivity implements View.OnClickListener{

    private Button mButton;
    private ProgressBar mProgressBar;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1: {
                    Log.d("Change","add");
                    mProgressBar.setProgress((Integer) msg.obj);
                    break;
                }
                case 2: {
                    AlertDialog.Builder dialog = new AlertDialog.Builder(ProgressBarTest.this);

                    dialog.setTitle("hint");
                    dialog.setMessage("Download Finish");
                    dialog.setCancelable(false);

                    //为AlertDialog界面添加OK按钮   hint:注意使用的方法名
                    dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    });

                    //为AlertDialog界面添加Cancel按钮   hint:注意使用的方法名
                    dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    });

                    dialog.show();
                    break;
                }
                default:
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.progressbar_layout);

        mButton = findViewById(R.id.btn_toDownload);
        mProgressBar = findViewById(R.id.progress_bar);

        mButton.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.btn_toDownload: {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int progress = mProgressBar.getProgress();


                        while (progress < 100) {
                            progress += 10;

                            Message msg = new Message();
                            msg.obj = progress;
                            msg.what = 1;
                            mHandler.sendMessage(msg);

                            try {
                                Thread.sleep(30);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }

                        mHandler.sendEmptyMessage(2);
                    }
                }).start();
                break;
            }
            default:
        }
    }
}
